package com.gexiaobao.pigeon.ui.fragment.login

import android.os.Bundle
import androidx.lifecycle.lifecycleScope
import com.gexiaobao.pigeon.R
import com.gexiaobao.pigeon.app.base.BaseFragment
import com.gexiaobao.pigeon.app.ext.countDownCoroutines
import com.gexiaobao.pigeon.app.ext.initClose
import com.gexiaobao.pigeon.app.ext.showMessage
import com.gexiaobao.pigeon.app.model.param.GetVerCodeParams
import com.gexiaobao.pigeon.app.model.param.ResetPwdParams
import com.gexiaobao.pigeon.app.util.RxToast
import com.gexiaobao.pigeon.app.util.Util
import com.gexiaobao.pigeon.databinding.FragmentForgotPsdBinding
import com.gexiaobao.pigeon.viewmodel.LoginViewModel
import me.hgj.jetpackmvvm.ext.nav
import me.hgj.jetpackmvvm.ext.parseState
import me.hgj.jetpackmvvm.ext.util.setOnclickNoRepeat

/**
 * @Author      : hxw
 * @Date        : 2023/5/31 11:33
 * @Describe    : 忘记密码
 */
class RetrievePasswordFragment : BaseFragment<LoginViewModel, FragmentForgotPsdBinding>() {

    override fun initView(savedInstanceState: Bundle?) {
        mDatabind.includeBar.toolbar.initClose("重置密码") {
            nav().navigateUp()
        }
        mDatabind.viewmodel = mViewModel
        mViewModel.getVerificationCode.set("获取验证码")
    }

    override fun onBindViewClick() {
        super.onBindViewClick()
        setOnclickNoRepeat(mDatabind.forgetCommit, mDatabind.forgetVerifyCodeSend) {
            when (it.id) {

                R.id.forget_commit -> {
                    changePwd()
                }

                R.id.forget_verify_code_send -> {
                    getVerCode()
                }
            }
        }
    }

    private fun getVerCode() {
        when {
            mViewModel.phoneNum.get().isEmpty() -> showMessage(resources.getString(R.string.user_mobile_number))
            !Util.isTelPhoneNumber(mViewModel.phoneNum.get()) -> showMessage(resources.getString(R.string.user_phone_number_wrong))
            else -> {
                //开启倒计时60s
                job = countDownCoroutines(60, {
                    mViewModel.getVerificationCode.set("${it}s")
                }, {
                    mViewModel.getVerificationCode.set("获取验证码")
                }, lifecycleScope)
                val body = GetVerCodeParams(mViewModel.phoneNum.get(), 2)
                mViewModel.getVerCode(body)
            }
        }
    }

    private fun changePwd() {
        when {
            mViewModel.phoneNum.get()
                .isEmpty() -> showMessage(resources.getString(R.string.user_mobile_number))

            !Util.isTelPhoneNumber(mViewModel.phoneNum.get()) -> showMessage(resources.getString(R.string.user_phone_number_wrong))
            mViewModel.verificationCode.get()
                .isEmpty() -> showMessage(resources.getString(R.string.user_verification_code))

            mViewModel.verificationCode.get().length != 6 -> showMessage(resources.getString(R.string.user_verification_code_wrong))
            mViewModel.password.get()
                .isEmpty() -> showMessage(resources.getString(R.string.user_password))

            mViewModel.password2.get()
                .isEmpty() -> showMessage(resources.getString(R.string.user_repeat_password))

            mViewModel.password.get() != mViewModel.password2.get() -> showMessage(
                resources.getString(
                    R.string.user_password_not_alike
                )
            )

            else -> {
                val body = ResetPwdParams(
                    mViewModel.phoneNum.get(),
                    mViewModel.verificationCode.get(),
                    mViewModel.password.get()
                )
                mViewModel.reSetPwd(body)
            }

        }
    }

    override fun createObserver() {
        super.createObserver()
        mViewModel.mResultReSet.observe(viewLifecycleOwner) { resultState ->
            parseState(resultState, {
                nav().navigateUp()
            }, {
                RxToast.showToast(it.msg)
            })
        }
    }
}